package hotel.controller;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;

import java.io.IOException;

import hotel.common.MsgBox;
import hotel.controller.order.CheckOutController;
import hotel.controller.room.RoomRentController;
import hotel.controller.room.RoomReserveController;
import hotel.controller.user.ChangeUserPasswdController;
import hotel.modal.Room;
import hotel.modal.User;
import javafx.event.ActionEvent;

import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.layout.BorderPane;

import javafx.scene.control.Menu;

public class MainSceneController {
	@FXML
	private BorderPane mainPane;
	@FXML
	private MenuItem menuLogout;
	@FXML
	private MenuItem menuQuit;
	@FXML
	private Menu menuManage;
	@FXML
	private MenuItem menuEmployeeManage;
	@FXML
	private MenuItem menuRoomManage;
	@FXML
    private MenuItem menuRentChangeManage;
	@FXML
	private MenuItem menuLoginManage;
	@FXML
	private MenuItem menuChangePassword;
	@FXML
	private MenuItem menuAbout;
	@FXML
	private FlowPane topFlowPane;
	@FXML
	private Button btnPandect;
	@FXML
	private Button btnRoomReserve;
	@FXML
	private Button btnRoomRent;
	@FXML
	private Button btnRoomOrder;
	@FXML
	private Button btnReserveManage;
	@FXML
	private Button btnRentManage;
	@FXML
	private Button btnOrderManage;
	@FXML
	private Button btnCustomerManage;
	@FXML
	private Button btnStat;
	
	private VBox vbox;
	private ScrollPane scrollPane;
	private TilePane tilePane;
	private MainCenterPageController tcontroller;
	
	User user = null;
	
	@FXML
	public void initialize() {
		// 加载数据
		loadData();
	}
	
	private void loadData() {
		mainPane.setCenter(null);
		try {
			FXMLLoader tfxmlLoader = new FXMLLoader(getClass().getResource("/hotel/view/MainCenterPage.fxml"));
			BorderPane borderPane = (BorderPane)tfxmlLoader.load();
			tcontroller = tfxmlLoader.<MainCenterPageController>getController();
			tcontroller.setInitData();
			mainPane.setCenter(borderPane);
			vbox = (VBox) borderPane.getCenter();
			scrollPane = (ScrollPane) vbox.getChildren().get(1);
			tilePane = (TilePane) scrollPane.getContent();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void setInitData(User user) {
		this.user = user;
		Stage stage = (Stage) mainPane.getScene().getWindow();
		if(user != null) {
			user.setModified(false);
			stage.setTitle(stage.getTitle() + " - " + (user.isAdmin()?"操作管理员(":"普通操作员(") + user.getUserName() + ")");
		}
//		if(user == null || (user != null && !user.isAdmin())) {
//			removeAdminContent();
//		}
	}
	
//	public void removeAdminContent() {
//		topFlowPane.getChildren().remove(btnEmployeeManage);
//		topFlowPane.getChildren().remove(btnRoomManage);
//	}
//	
//	public void addAdminContent() {
//		removeAdminContent();
//		topFlowPane.getChildren().add(5, btnEmployeeManage);
//		topFlowPane.getChildren().add(6, btnRoomManage);
//	}

	// Event Listener on MenuItem[#menuLogout].onAction
	@FXML
	public void onMenuLogoutAction(ActionEvent event) {
		Stage stage = (Stage) mainPane.getScene().getWindow();
		stage.hide();
		user = new User();
		try {
			FXMLLoader tfxmlLoader = new FXMLLoader(getClass().getResource("/hotel/view/Login.fxml"));
			Parent tRoot = (Parent)tfxmlLoader.load();
			LoginController tcontroller = tfxmlLoader.<LoginController>getController();
			tcontroller.setInitData(user);
			Stage tStage = new Stage();
			tStage.setTitle("小型客房管理系统登录");
			tStage.setScene(new Scene(tRoot));
			tStage.showAndWait();
			tStage.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		int index = stage.getTitle().indexOf(" - ");
		stage.setTitle(stage.getTitle().substring(0, index));
		stage.setTitle(stage.getTitle() + " - " + (user.isAdmin()?"操作管理员(":"普通操作员(") + user.getUserName() + ")");
		if(user.isModified()) { // 如果用户验证成功再次加载主页面
			// 如果登录的是管理员，将管理员的控制内容加上 ,否则除去
//			if(user.isAdmin()) addAdminContent(); 
//			else removeAdminContent();
			// 显示主页面
			stage.show();
		}else {
			stage.close();
		}
	}
	
	// Event Listener on MenuItem[#menuQuit].onAction
	@FXML
	public void onMenuQuitAction(ActionEvent event) {
		Stage stage = (Stage) mainPane.getScene().getWindow();
		stage.close();
	}
	
	// Event Listener on MenuItem[#menuEmployeeManage].onAction
	@FXML
	public void onMenuEmpManageAction(ActionEvent event) {
		if(user == null || !user.isAdmin()) {
			MsgBox.showInformationDialog("温馨提示", null, "很抱歉，您不是操作管理员，无法使用此功能！");
			return ;
		}
		
		try {
			Stage stage = new Stage();
			Parent root = (Parent)FXMLLoader.load(getClass().getResource("/hotel/view/employee/EmployeeManage.fxml"));
			stage.setScene(new Scene(root));
			stage.setTitle("员工管理模块");
			stage.initModality(Modality.APPLICATION_MODAL);
			stage.show();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// Event Listener on MenuItem[#menuRoomManage].onAction
	@FXML
	public void onMenuRoomManageAction(ActionEvent event) {
		if(user == null || !user.isAdmin()) {
			MsgBox.showInformationDialog("温馨提示", null, "很抱歉，您不是操作管理员，无法使用此功能！");
			return ;
		}
		
		try {
			Stage stage = new Stage();
			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/hotel/view/room/RoomManage.fxml"));
			Parent root = (Parent)fxmlLoader.load();
			stage.setScene(new Scene(root));
			stage.setTitle("房间管理模块");
			stage.initModality(Modality.APPLICATION_MODAL);
			stage.showAndWait();
			tcontroller.refresh();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// Event Listener on MenuItem[#menuRentChangeManage].onAction
	@FXML
    void onMenuRentChangeManageAction(ActionEvent event) {
		try {
			Stage stage = new Stage();
			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/hotel/view/room/RentChangeManage.fxml"));
			Parent root = (Parent)fxmlLoader.load();
			stage.setScene(new Scene(root));
			stage.setTitle("换房列表");
			stage.initModality(Modality.APPLICATION_MODAL);
			stage.show();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
	
	// Event Listener on MenuItem[#menuLoginManage].onAction
	@FXML
	public void onMenuLoginManageAction(ActionEvent event) {
		if(user == null || !user.isAdmin()) {
			MsgBox.showInformationDialog("温馨提示", null, "很抱歉，您不是操作管理员，无法使用此功能！");
			return ;
		}
		
		try {
			Stage stage = new Stage();
			Parent root = (Parent)FXMLLoader.load(getClass().getResource("/hotel/view/user/UserManage.fxml"));
			stage.setScene(new Scene(root));
			stage.setTitle("操作员管理");
			stage.initModality(Modality.APPLICATION_MODAL);
			stage.show();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// Event Listener on MenuItem[#menuChangePassword].onAction
	@FXML
	public void onMenuChangePasswordAction(ActionEvent event) {
		try {
			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/hotel/view/user/ChangeUserPasswd.fxml"));
			Stage stage = new Stage();
			Parent root = (Parent)fxmlLoader.load();
			ChangeUserPasswdController controller = fxmlLoader.getController();
			controller.setInitData(user);
			stage.setScene(new Scene(root));
			stage.setTitle("修改密码");
			stage.show();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// Event Listener on MenuItem[#menuAbout].onAction
	@FXML
	public void onMenuAboutAction(ActionEvent event) {
		try {
			Stage stage = new Stage();
			Parent root = (Parent)FXMLLoader.load(getClass().getResource("/hotel/view/About.fxml"));
			stage.setScene(new Scene(root));
			stage.setTitle("关于软件");
			stage.initModality(Modality.APPLICATION_MODAL);
			stage.show();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// Event Listener on Button[#btnPandect].onAction
	@FXML
	public void onBtnPandectAction(ActionEvent event) {
		tcontroller.refresh(); // 刷新，重新载入数据
	}
	
	// Event Listener on Button[#btnRoomReserve].onAction
	@FXML
	public void onBtnRoomReserveAction(ActionEvent event) {
		Room room = (Room) tilePane.getUserData();
		if(room != null && (room.isHourRoom() || room.getStatus() != 1)) { // 只有非钟点房和状态为可供的房间可以被预定
			MsgBox.showInformationDialog("温馨提示", null, "该房间不可以被预定！");
			return ;
		}
		try {
			Stage stage = new Stage();
			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/hotel/view/room/RoomReserve.fxml"));
			Parent root = (Parent)fxmlLoader.load();
			RoomReserveController controller = fxmlLoader.<RoomReserveController>getController();
			controller.setInitData(room);
			stage.setScene(new Scene(root));
			stage.setTitle("客房预定");
			stage.initModality(Modality.APPLICATION_MODAL);
			stage.showAndWait();
			room = controller.getRoom();
			if(room != null && room.isModified()) {
				tcontroller.refresh(); // 成功预定后会使房间状态改变，因此要重新载入房间信息。
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// Event Listener on Button[#btnRoomRent].onAction
	@FXML
	public void onBtnRoomRentAction(ActionEvent event) {
		Room room = (Room) tilePane.getUserData();
		if(room != null && (room.getStatus() > 2)) { // 只有状态为可供或者预定的房间才可以入住
			MsgBox.showInformationDialog("温馨提示", null, "该房间不可以入住！");
			return ;
		}
		try {
			Stage stage = new Stage();
			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/hotel/view/room/RoomRent.fxml"));
			Parent root = (Parent)fxmlLoader.load();
			RoomRentController controller = fxmlLoader.<RoomRentController>getController();
			controller.setUser(user);
			controller.setRoom(room);
			stage.setScene(new Scene(root));
			stage.setTitle("客户入住");
			stage.initModality(Modality.APPLICATION_MODAL);
			stage.showAndWait();
			room = controller.getRoom();
			if(room != null && room.isModified()) {
				tcontroller.refresh(); // 成功预定后会使房间状态改变，因此要重新载入房间信息。
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	// Event Listener on Button[#btnRoomOrder].onAction
	@FXML
	public void onBtnRoomOrder(ActionEvent event) {
		Room room = (Room) tilePane.getUserData();
		if(room == null) {
			MsgBox.showInformationDialog("温馨提示", null, "请选择一个房间！");
			return ;
		}
		
		if(room.getStatus() != 3) {
			MsgBox.showInformationDialog("温馨提示", null, "该房间不可以结账！");
			return ;
		}
		
		try {
			Stage stage = new Stage();
			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/hotel/view/order/CheckOut.fxml"));
			Parent root = (Parent)fxmlLoader.load();
			CheckOutController controller = fxmlLoader.<CheckOutController>getController();
			controller.setUser(user);	
			controller.setRoom(room);
			stage.setScene(new Scene(root));
			stage.setTitle("入住结账");
			stage.initModality(Modality.APPLICATION_MODAL);
			stage.showAndWait();
			room = controller.getRoom();
			if(room != null && room.isModified()) {
				tcontroller.refresh(); // 成功预定后会使房间状态改变，因此要重新载入房间信息。
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	// Event Listener on Button[#btnReserveManage].onAction
	@FXML
	public void onBtnReserveManageAction(ActionEvent event) {
		try {
			Stage stage = new Stage();
			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/hotel/view/room/RoomReserveManage.fxml"));
			Parent root = (Parent)fxmlLoader.load();
			stage.setScene(new Scene(root));
			stage.setTitle("房间预定管理");
			stage.initModality(Modality.APPLICATION_MODAL);
			stage.showAndWait();
			tcontroller.refresh(); // 重新载入房间信息
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// Event Listener on Button[#btnRentManage].onAction
	@FXML
	public void onBtnRentManageAction(ActionEvent event) {
		try {
			Stage stage = new Stage();
			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/hotel/view/room/RoomRentManage.fxml"));
			Parent root = (Parent)fxmlLoader.load();
			stage.setScene(new Scene(root));
			stage.setTitle("房间入住管理");
			stage.initModality(Modality.APPLICATION_MODAL);
			stage.showAndWait();
			tcontroller.refresh(); // 重新载入房间信息
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// Event Listener on Button[#btnOrderManage].onAction
	@FXML
	public void onBtnOrderManageAction(ActionEvent event) {
		try {
			Stage stage = new Stage();
			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/hotel/view/order/OrderManage.fxml"));
			Parent root = (Parent)fxmlLoader.load();
			stage.setScene(new Scene(root));
			stage.setTitle("结账单管理");
			stage.initModality(Modality.APPLICATION_MODAL);
			stage.show();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// Event Listener on Button[#btnCustomerManage].onAction
	@FXML
	public void onBtnCustomerManageAction(ActionEvent event) {
		try {
			Stage stage = new Stage();
			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/hotel/view/customer/CustomerManage.fxml"));
			Parent root = (Parent)fxmlLoader.load();
			stage.setScene(new Scene(root));
			stage.setTitle("客户信息管理");
			stage.initModality(Modality.APPLICATION_MODAL);
			stage.show();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// Event Listener on Button[#btnStat].onAction
	@FXML
	public void onBtnStatAction(ActionEvent event) {
		try {
			Stage stage = new Stage();
			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/hotel/view/Statistics.fxml"));
			Parent root = (Parent)fxmlLoader.load();
			stage.setScene(new Scene(root));
			stage.setTitle("统计");
			stage.initModality(Modality.APPLICATION_MODAL);
			stage.show();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
